const app = {
  state: {
    ios: true,
    sessionStorageLists: null,
    loginInfo: null,
    workNumber: null,
    originSchemeInfo: [],
    currentIndex: null,
    formInfo: {},
    schemes: [],
    currentInsurances: [],
    currentScheme: null,
    currentDuties: [],
    vocations: [],
    submitFlag: null,
    weChatCode: null,
    currentType: null,
    vocationsInfos: null,
    fastenUnit: null,
    target: null,
    submitRiskNo: null,
    changeStatus: null,
    appoints: [],
    minutedInfo: null,
    wxFlag: null,
    currentDelDuty: null,
    allAppoints: null,
    transition: 'left',
    submitFailedNotes:null
  },
  mutations: {
    CHANGE_TRANSITION: (state, flag) => {
      "use strict";
      state.transition = flag
    },
    SAVE_WX_FLAG: (state, flag) => {
      "use strict";
      state.wxFlag = flag
    },
    SAVE_MINUTED_INFO: (state, info) => {
      "use strict";
      state.minutedInfo = info
    },
    SAVE_ONCE_GET_LISTS: (state, info) => {
      state.sessionStorageLists = info
    },
    SAVE_LOGIN_INFO: (state, info) => {
      state.loginInfo = info
    },
    SET_SCHEMES_INFO: (state, info) => {
      state.originSchemeInfo = info;
    },
    SET_FORM_INFO: (state, info) => {
      state.formInfo = info
    },
    SET_CURRENT_SCHEME_INFO: (state, info) => {
      state.currentScheme = info
    },
    INIT_CURRENT_INDEX: (state, index) => {
      state.currentIndex = index
    },
    SAVE_NEW_SCHEME: (state, info) => {
      state.schemes.push(info)
    },
    SAVE_HAD_TARGET: (state, target) => {
      state.target = target
    },
    SAVE_NEW_INSURANCES: (state, info) => {
      state.currentInsurances.push(info);
      state.currentScheme.duties = state.currentInsurances
    },
    SAVE_CURRENT_DUTIES: (state, duties) => {
      state.currentDuties = duties
    },
    REMOVE_INSURANCE: (state) => {
      state.currentInsurances = [];
      state.currentScheme.duties = []
    },
    SAVE_CURRENT_MODIFY_DUTY: (state, duty) => {
      "use strict";
      state.currentDelDuty = duty
    },
    REMOVE_SOME_INSURANCE: (state, obj) => {
      state.currentDuties.push(obj.removedDuty);
      state.currentInsurances.splice(obj.removeIndex, 1);
    },
    REMOVE_SOME_SCHEME: (state, index) => {
      let target = state.originSchemeInfo[index].insuredTarget;
      if (target === "A") {
        state.target = "B"
      } else if (target === "B") {
        state.target = "A"
      }
      state.originSchemeInfo.splice(index, 1);
      if (state.originSchemeInfo.length === 0) {
        state.target = null
      }
    },
    SAVE_PREV_VOCATION_KIND: (state, kind) => {
      let flag = true;
      if (state.vocations.length > 0) {
        for (let i = 0; i < state.vocations.length; i++) {
          if (kind.text === state.vocations[i].text) {
            flag = false;
            break
          }
        }
      } else {
        flag = true
      }
      if (flag) {
        let vocations = state.vocations;
        vocations.push(kind);
        let rule = (a, b) => Number(a.value) - Number(b.value);
        vocations.sort(rule);
        state.vocations = vocations
      }
    },
    SET_SUBMIT_RESULT: (state, status) => {
      state.submitFlag = status.status;
      state.submitRiskNo = status.riskAssessNo;
      state.submitFailedNotes = status.reasonNotes
    },
    SET_GRPTYPE: (state, type) => {
      state.currentType = type;
    },
    REMOVE_SUBMIT_FLAG: (state) => {
      state.submitFlag = null
    },
    REMOVE_SCHEME: (state) => {
      state.currentScheme = null
    },
    REMOVE_HAD_INSURANCES: (state) => {
      state.currentInsurances = []
    },
    REMOVE_HAD_APPOINTS: (state) => {
      state.appoints = []
    },
    INIT_FORM: (state) => {
      state.originSchemeInfo = [];
      state.currentIndex = null;
      state.formInfo = {};
      state.schemes = [];
      state.currentInsurances = [];
      state.currentScheme = null;
      state.vocations = [];
      state.submitFlag = null;
      state.currentType = null;
      state.currentDuties = [];
      state.vocationsInfos = null;
      state.fastenUnit = null;
      state.target = null;
      state.changeStatus = null;
      state.appoints = [];
      state.currentDelDuty = null;
      state.submitFailedNotes = null
    },
    INIT_GRP_ABOUT: (state) => {
      state.originSchemeInfo = [];
      state.schemes = [];
      state.currentType = null;
      state.vocations = [];
      state.currentDuties = [];
      state.currentInsurances = [];
      state.currentScheme = null;
      state.currentIndex = null;
      state.vocationsInfos = null;
      state.target = null;
      state.appoints = [];
      state.currentDelDuty = null;
      state.submitFailedNotes = null
    },
    INIT_VOCATION_ABOUT: (state) => {
      state.originSchemeInfo = [];
      state.schemes = [];
      state.vocations = [];
      state.currentType = null;
      state.currentDuties = [];
      state.currentInsurances = [];
      state.currentScheme = null;
      state.currentIndex = null;
      state.target = null;
      state.appoints = [];
      state.currentDelDuty = null;
      state.submitFailedNotes = null;
    },
    GET_WECHAT_CODE: (state, code) => {
      state.weChatCode = code
    },
    CREATE_VOCATION_TYPES: (state, infos) => {
      state.vocationsInfos = infos
    },
    REMOVE_VOCATION_KIND: (state, vocation) => {
      let vocations = state.vocations;
      for (let i = 0; i < vocations.length; i++) {
        if (vocation.occupationCode === vocations[i].value) {
          vocations.splice(i, 1)
        }
      }
      state.vocations = vocations
    },
    GET_WORK_NUMBER: (state, number) => {
      state.workNumber = number
    },
    GET_IOS: (state, flag) => {
      state.ios = flag
    },
    SAVE_FASTEN_UNIT: (state, object) => {
      state.fastenUnit = object
    },
    SAVE_CHANGE_STATUS: (state, status) => {
      state.changeStatus = status
    },
    GET_SPECIAL_APPOINTS: (state, appoints) => {
      "use strict";
      state.allAppoints = appoints
    },
    SAVE_SPECIAL_APPOINTS: (state, appoints) => {
      state.appoints = appoints;
      state.currentScheme.specialAppoints = appoints
    }
  },
  actions: {
    initForm: ({commit}) => {
      commit('INIT_FORM')
    },
    removeScheme: ({commit}) => {
      commit('REMOVE_SCHEME')
    },
    getWeChat: ({commit}, code) => {
      commit('GET_WECHAT_CODE', code)
    },
    getWorkNumber: ({commit}, number) => {
      commit('GET_WORK_NUMBER', number)
    },
    getIos: ({commit}, flag) => {
      commit('GET_IOS', flag)
    },
  }
}
export default app;
